const commonUrl = "http://xjk.minshangkeji.com/index.php"; //公共路径 

function commonRequest(option) {
	if(option.needLogin == 1) {
		var userInfo = uni.getStorageSync('loginInfo');
		if(!userInfo) {
			uni.showToast({
			    icon:"none",
			    title: '此操作需要您登录,即将前往登录页面。',
			   })
			   setTimeout(function(){
			    uni.navigateTo({
			     url: '/pages/login/login'
			    })
			   },1500)
			   return;
		}
	}
	var requestData = {};
	if (option.data) {
		requestData = option.data;
	}
	var userInfo = uni.getStorageSync('loginInfo');
	if (userInfo) {
		var userToken = userInfo;
		requestData = Object.assign(requestData, {
			token: userToken
		})
	}
	var method = 'GET';
	if(option.method) {
		method = option.method;
	}
	var header = {
		'Content-type': 'application/x-www-form-urlencoded',
	};
	if (option.header) {
		header = option.header;
	}
	uni.request({
		url: commonUrl + option.url,
		data: requestData,
		header: header,
		method: method,
		success: function(ret) {
			if (option.success) {
				option.success(ret);
			}
		},
		fail: function(err) {
			if (option.err) {
				option.success(err);
			}
		}
	})
}

module.exports = {
	commonRequest: commonRequest
};
